<?php

/***
 * Class WidgetFactory is an xml factory that allows a developer to easily create
 * commonly used html elements for display or xsl parsing.
 ***/
class WidgetFactory extends GlobalFactory {

   // ! Constructor Method

   /**
	* This prevents the system from instantiating this class as it
	* is meant to be used statically.
	*
	* @param none
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Private
	* @return Void
	*/
	private function __construct() { /* empty */ }


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function form($action, XmlElement $content = null, $attributes = array(), $id = '', $method = 'post')
	{
		$attributes['action'] = $action;
		$attributes['method'] = $method;

		if($id)
		{
			$attributes['id'] = $id;
		}

		$Form = new XmlElement('form', null, $attributes);

		if(get_class($content) == 'XmlElement')
		{
			$Form->addNode($content);
		}

		return $Form;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function input($name, $value, $type = 'text', $id = '', $attributes = array())
	{
		$attributes['type'] = $type;
		$attributes['name'] = $name;

		if($id)
		{
			$attributes['id'] = $id;
		}

		$attributes['value'] = $value;

		return new XmlElement('input', null, $attributes);
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function textarea($name, $value = '', $id = '', $attributes = array())
	{
		$attributes['name'] = $name;

		if($id)
		{
			$attributes['id'] = $id;
		}

		$TextArea = new XmlElement('textarea', $value, $attributes);

		$TextArea->isSelfClosing(false);

		return $TextArea;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function label($title = '', $for = '', XmlElement $content = null, $attributes = array())
	{
		if($for)
		{
			$attributes['for'] = $for;
		}

		$Label = new XmlElement('label', $title, $attributes);

		if($content)
		{
			$Label->addNode($content);
		}

		return $Label;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function anchor($href, $content, $id = '', $attributes = array())
	{
		$attributes['href'] = $href;

		if($id)
		{
			$attributes['id'] = $id;
		}

		if(get_class($content) == 'XmlElement')
		{
			$Anchor = new XmlElement('a', null, $attributes);

			$Anchor->addNode($content);
		}
		else
		{
			$Anchor = new XmlElement('a', $content, $attributes);
		}

		return $Anchor;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function select($name, $options = array(), $id = '', $attributes = array())
	{
		$attributes['name'] = $name;

		if($id)
		{
			$attributes['id'] = $id;
		}

		$Select = new XmlElement('select', null, $attributes);

		foreach($options as $option)
		{
			$Option = new XmlElement('option', $option['desc']);

			$Option->setAttribute('value', $option['value']);
			$Option->isSelfClosing(false);

			$Select->addNode($Option);
		}

		$Select->isSelfClosing(false);

		return $Select;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function table(XmlElement $thead = null, XmlElement $tbody = null, $id = '', $attributes = array())
	{
		if($id)
		{
			$attributes['id'] = $id;
		}

		$Table = new XmlElement('table', null, $attributes);

		$Table->addNode($thead);
		$Table->addNode($tbody);

		return $Table;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function thead($data = array(), $id = '', $attributes = array())
	{
		$Thead = new XmlElement('thead', null, $attributes);

		$Tr = new XmlElement('tr');

		foreach($data as $Th)
		{
			if(is_object($Th) && get_class($Th) == 'XmlElement')
			{
				$Tr->addNode($Th);
			}
		}

		$Thead->addNode($Tr);

		return $Thead;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function th($content = null, $attributes = array())
	{
		if(get_class($content) == 'XmlElement')
		{
			$Th = new XmlElement('th', null, $attributes);

			$Th->addNode($content);
		}
		else
		{
			$Th = new XmlElement('th', $content, $attributes);
		}

		return $Th;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function tbody($data, $attributes = array())
	{
		$Tbody = new XmlElement('tbody', null, $attributes);

		foreach($data as $table_row)
		{
			$Tr = new XmlElement('tr');

			foreach($table_row as $Td)
			{
				if(is_object($Td) && get_class($Td) == 'XmlElement')
				{
					$Tr->addNode($Td);
				}
			}

			$Tbody->addNode($Tr);
		}

		return $Tbody;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function tr($content = null, $attributes = array())
	{
		if(get_class($content) == 'XmlElement')
		{
			$Tr = new XmlElement('tr', null, $attributes);

			$Tr->addNode($content);
		}
		else
		{
			$Tr = new XmlElement('tr', $content, $attributes);
		}

		return $Tr;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function td($content = null, $attributes = array())
	{
		if(get_class($content) == 'XmlElement')
		{
			$Td = new XmlElement('td', null, $attributes);

			$Td->addNode($content);
		}
		else
		{
			$Td = new XmlElement('td', $content, $attributes);
		}

		return $Td;
	}


   // ! Executor Method

   /**
	* Used in combination with GlobalXmlFactory::generateErrorXml. It allows
	* the developer to auto-complete forms if errors are found after submitting them.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Object
	*/
	static public function error(XmlElement $element, $message, $attributes = array())
	{
		$Div = new XmlElement('div', null, $attributes);

		$Div->addNode(new XmlElement('p', $message));

		$Div->addNode($element);

		return $Div;
	}
}

?>